package net.ib.mtalk.ui.setting.user;

import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.AccountAuthToken;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenRes;
import net.ib.mntalk.protocol.Session.RegisterAccountReq;
import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseActivity;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.global.GlobalPreferences;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MakeIdActivity extends BaseActivity 
{
	private EditText mEditId;
	private EditText mEditPw;
	private EditText mEditRePw;
	private Button mBtnSave;
	private TextView mTextId;
	
	private Context mContext;
	private String mType;
	
	private MakeIdController mUiController;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS", Toast.LENGTH_SHORT).show();
				
				AcquireAccountAuthTokenRes res = (AcquireAccountAuthTokenRes) msg.obj;
				AccountAuthToken accountAuthToken = res.getAccountAuthToken();
				
				mUiController.acquireAccountToServer(accountAuthToken);
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_UNKNOWN_ERROR:
				Toast.makeText(mContext, "HANDLER_UNKNOWN_ERROR", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_SUCCESS:
				RegisterAccountReq registerAccountReq = new RegisterAccountReq();
				registerAccountReq.setAccountToken((AccountToken)msg.obj);
				registerAccountReq.setAccessToken(GlobalPreferences.getMessageFromPreferences(AccessToken.class));
				registerAccountReq.setUserUuid(GlobalPreferences.getUserUUID());
				mUiController.setUserAccount(registerAccountReq);
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_FAIL:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_REGISTER_ACCOUNT_SUCCESS:
				Toast.makeText(mContext, "HANDLER_REGISTER_ACCOUNT_SUCCESS", Toast.LENGTH_SHORT).show();
				setResult(RESULT_OK, getIntent());
				finish();
				break;
			case HandlerMsgConst.HANDLER_REGISTER_ACCOUNT_FAIL:
				Toast.makeText(mContext, "HANDLER_REGISTER_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_make_id);
		mContext = this;
		mUiController = new MakeIdController(this, mHandler);
		mType = getIntent().getStringExtra("type");
		initView();
	}
	
	private void initView()
	{
		mEditId = (EditText)findViewById(R.id.make_id);
		
		mTextId = (TextView)findViewById(R.id.make_id_text);
		
		if(mType.equals("make"))
		{
			mEditId.setVisibility(View.VISIBLE);
			mTextId.setVisibility(View.GONE);
		}
		else if(mType.equals("modify"))
		{
			mEditId.setVisibility(View.GONE);
			mTextId.setVisibility(View.VISIBLE);
			mTextId.setText(getIntent().getStringExtra("id"));
		}
		
		mEditPw = (EditText)findViewById(R.id.make_pw);
		
		mEditRePw = (EditText)findViewById(R.id.make_reenter_pw);
		
		mBtnSave = (Button)findViewById(R.id.save_btn);
		mBtnSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String id = null;
				if(mType.equals("make"))
					id = mEditId.getText().toString();
				else if(mType.equals("modify"))
					id = mTextId.getText().toString();
				String pw = mEditPw.getText().toString();
				String rePw = mEditRePw.getText().toString();
				
				if(id.length() > 3 && pw.length() > 5 && rePw.length() > 5 && pw.equals(rePw) && !id.equals(getString(R.string.setting_profile_create_account)))
				{
					mUiController.acquireAccountAuthToServer(id, pw);
				}
				else
					Toast.makeText(mContext, "Wrong", Toast.LENGTH_SHORT).show();
			}
		});
	}
}
